<?
if(!defined("B_PROLOG_INCLUDED") || B_PROLOG_INCLUDED!==true) die();

if(!isset($arParams["CACHE_TIME"]))
	$arParams["CACHE_TIME"] = 3600;

$arParams["IBLOCK_TYPE"] = trim($arParams["IBLOCK_TYPE"]);
if(strlen($arParams["IBLOCK_TYPE"])<=0)
 	$arParams["IBLOCK_TYPE"] = "lectures";
if($arParams["IBLOCK_TYPE"]=="-")
	$arParams["IBLOCK_TYPE"] = "";

if ($_SERVER["REQUEST_METHOD"]=="POST")
{
    if ($_REQUEST["FORMTYPE"]=="signup") // запись на вакансию
    {       
        $uid = $USER->GetID();

        $el = new CIBlockElement;
            $PROP = array();
            
            if ($_FILES["file"]["error"]==0)
                $PROP["FILE"][] = $_FILES["file"];
            $PROP["VACANCY"] = $_REQUEST["VACANCY_ID"];
            
            $arLoadArray = Array(
                "MODIFIED_BY"       => $uid,
                "IBLOCK_SECTION"    => false,
                "PROPERTY_VALUES"   => $PROP,
                "IBLOCK_ID"         => $arParams["IBLOCK_LOGS_ID"],
                "NAME"              => $_REQUEST["VACANCY_ID"]." - ".$uid,
                "ACTIVE"            => "Y",
            );
        $el->Add($arLoadArray);        
    }
}

if($this->StartResultCache(false, array($USER->GetID())))
{
    if(!CModule::IncludeModule("iblock"))
    {
        $this->AbortResultCache();
        ShowError("Модуль Информационных блоков не установлен");
        return;
    }
       
    $arResult = array();

   
    // *************************************************************************
    // выбираем список активных вакансий
    // *************************************************************************
    $arSelect = array(
        "ID",
        "NAME",
        "PREVIEW_TEXT",
    );

    $arFilter = array (
        "IBLOCK_TYPE" => $arParams["IBLOCK_TYPE"],
        "IBLOCK_ID"=> $arParams["IBLOCK_VACANCY_ID"],
        "ACTIVE" => "Y",
    );

    $arOrder = array(
        "SORT"=>"ASC",
    );

    $rsItems = CIBlockElement::GetList($arOrder, $arFilter, false, false, $arSelect);

    while ($item = $rsItems->GetNext())
    {
        if ($USER->IsAuthorized())
            $item["CAN_SIGNUP"] = 1;
        else
            $item["CAN_SIGNUP"] = 0;
        
        $uid = $USER->GetID();
        
        // выбор уже подавших заявки
        $arSelect = array(
            "ID",
            "CREATED_BY",
            "PROPERTY_FILE"
        );

        $arFilter = array (
            "IBLOCK_TYPE" => $arParams["IBLOCK_TYPE"],
            "IBLOCK_ID"=> $arParams["IBLOCK_LOGS_ID"],
            "ACTIVE" => "Y",
            "PROPERTY_VACANCY" => $item["ID"]
        );

        $arOrder = array(
            "DATE_CREATE"=>"ASC",
        );

        $rsItems2 = CIBlockElement::GetList($arOrder, $arFilter, false, false, $arSelect);
        while ($item2 = $rsItems2->GetNext())
        {
            if ($item2["CREATED_BY"] == $USER->GetID())
                $item["CAN_SIGNUP"] = 0;
            
            $rsUser = CUser::GetByID($item2["CREATED_BY"]);
            if ($arUser = $rsUser->Fetch())
                $item2["APPLICANT"] = $arUser["NAME"]." ".$arUser["LAST_NAME"]; 

            if ($uid==1 || $uid==65 || $uid==19)
            {
                $item2["FILE"] = CFile::GetFileArray($item2["PROPERTY_FILE_VALUE"]);
            }
            
            $item["APPLICANTS"][] = $item2;
        }                      
        
        
        $arResult[] = $item;
    }
    // *************************************************************************
    // *************************************************************************
//echo "<pre>";
//print_r($arResult);
//echo "</pre>";

    $this->IncludeComponentTemplate();
}
?>
